package it.koutifaris.travianmap.dal.sqlservices.travianserver;

import static it.koutifaris.travianmap.dal.sqlservices.travianserver.TravianServerContract.DB_NAME;
import it.koutifaris.travianmap.dal.sqlservices.travianserver.TravianServerContract.Settings;
import it.koutifaris.travianmap.dal.sqlservices.travianserver.TravianServerContract.TravianServers;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TravianServerDbHelper extends SQLiteOpenHelper {

	private static final int DB_VERSION = 1;

	public TravianServerDbHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(getTravianServersCreationQuery());
		db.execSQL(getSettingsCreationQuery());
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(getTravianServersDropQuery());
		db.execSQL(getSettingDropQuery());

		onCreate(db);
	}

	private String getTravianServersDropQuery() {
		return "DROP TABLE IF EXISTS " + TravianServers.TABLE_NAME;
	}

	private String getSettingDropQuery() {
		return "DROP TABLE IF EXISTS " + Settings.TABLE_NAME;
	}

	private String getTravianServersCreationQuery() {
		return "CREATE TABLE " + TravianServers.TABLE_NAME + " ("
				+ TravianServers.COLUMN_NAME_SERVER + " TEXT PRIMARY KEY,"
				+ TravianServers.COLUMN_NAME_LAST_UPDATE + " DATE"
				+ ")";
	}

	private String getSettingsCreationQuery() {
		return "CREATE TABLE " + Settings.TABLE_NAME + " ("
				+ Settings.COLUMN_NAME_SERVER + " TEXT PRIMARY KEY,"
				+ Settings.COLUMN_NAME_AUTOUPDATE + " BOOLEAN,"
				+ Settings.COLUMN_NAME_WIFI_ONLY + " BOOLEAN"
				+ ");";
	}

}
